#include"touch.h"

void *lcd_check(void *arg)
{
    while(1)
    {
		lcd_show_color(0x0000FF00);
		sleep(1);

		lcd_show_color(0x00FF0000);
		sleep(1);

		lcd_show_color(0x000000FF);
		sleep(1);
    }
}
void *media_control(void *arg)
{
		 char *bmp_name;
     bmp_name = "music.bmp"; 
     lcd_show_pic(bmp_name);

}
void *music_control(void *arg)
{
		 char *bmp_name;
     bmp_name = "music.bmp"; 
     lcd_show_pic(bmp_name);

}
void *beep_control(void *arg)
{
	 char *bmp_name;
     bmp_name = "beep_ctrl.bmp"; 
     lcd_show_pic(bmp_name);
}
void *led_ctrl(void *arg)
{
		  char *bmp_name;
     bmp_name = "led_ctrl.bmp"; 
     lcd_show_pic(bmp_name);
     
}
void *lcd_disaplay(void *arg)
{
  while(1)
    {
		  char *bmp_name;
    
     bmp_name = "led_ctrl.bmp"; 
    lcd_show_pic(bmp_name);
    sleep(1);
      bmp_name = "music.bmp"; 
     lcd_show_pic(bmp_name);
  
       sleep(1);
    
    }
}
int lcd_show_color(unsigned int color)
{
    int fd_lcd;
    int *lcd_base = NULL;
    int x, y;

    fd_lcd = open("/dev/fb0", O_RDWR);
    if (fd_lcd == -1)
    {
        printf("lcd open failed\n");
        return -1;
    }

    lcd_base = mmap(NULL, 800 * 480 * 4, PROT_READ | PROT_WRITE, MAP_SHARED, fd_lcd, 0);

    if (lcd_base == MAP_FAILED)
    {
        printf("lcd mmap failed\n");
        return -1;
    }

    for (y = 0; y < 480; y++)
    {
        for (x = 0; x < 800; x++)
        {
            *(lcd_base + 800 * y + x) = color; //green
        }
    }

    munmap(lcd_base, 800 * 480 * 4);
    close(fd_lcd);

    return 0;
}
int lcd_show_pic(char *bmp_name)
{
    int fd_lcd;
    int fd_bmp;
    int *lcd_base = NULL;
    int x, y, n = 0;
    char bmp_buf[800 * 480 * 3];
	
    char path[100] = "/home/"; // 拼接路径
    strcat(path, bmp_name); // 将输入的图片文件名添加到路径后面

    fd_bmp = open(path, O_RDWR);
    if (fd_bmp == -1)
    {
        printf("bmp open failed\n");
        return -1;
    }

    lseek(fd_bmp, 54, SEEK_SET);
    read(fd_bmp, bmp_buf, 800 * 480 * 3);
    close(fd_bmp);

    fd_lcd = open("/dev/fb0", O_RDWR);
    if (fd_lcd == -1)
    {
        printf("lcd open failed\n");

        return -1;
    }

    lcd_base = mmap(NULL, 800 * 480 * 4, PROT_READ | PROT_WRITE, MAP_SHARED, fd_lcd, 0);

    if (lcd_base == MAP_FAILED)
    {
        printf("lcd mmap failed\n");
        return -1;
    }

    for (y = 0; y < 480; y++)
    {
        for (x = 0; x < 800; x++)
        {
            *(lcd_base + 800 * (479 - y) + x) = (bmp_buf[3 * n + 2] << 16) + (bmp_buf[3 * n + 1] << 8) + (bmp_buf[3 * n] << 0);
            n++;
        }
    }
    return 0;
    }
struct touch_data get_touch_xy(void)
{
struct touch_data xy = {0,0};

struct input_event ts_data;
unsigned char i=0;
int x=0,y=0;
	//1.打开触摸屏的文件
	int fd;
	fd = open("/dev/input/event0", O_RDONLY);
	if(fd == -1)
	{
		printf("open touch file err!\r\n");
		return xy;
	}

	for(i=0;i<6;i++)
	{
		//2.读取触摸屏的数据
				read(fd, &ts_data, sizeof(ts_data));
		if(ts_data.type==EV_ABS)//读取触摸屏数据
		{
			if(ts_data.code == ABS_X)
				xy.x=ts_data.value;
			else if(ts_data.code == ABS_Y)
				xy.y=ts_data.value;
		}
	}



		//4.关闭触摸屏
	close(fd);
	return xy;
	}
